class Node:
    def __init__(self, name):
        self.name = name  # 节点名称（文件或文件夹名）
        self.parent = None  # 父节点引用

    def get_path(self):
        path = []
        node = self
        while node is not None:
            path.append(node.name)  # 收集路径部分
            node = node.parent  # 移动到父节点
        return '/'.join(reversed(path))  # 返回完整路径

class File(Node):
    def __init__(self, name, content=''):
        super(File, self).__init__(name)  # 初始化父类属性
        self.content = content  # 文件内容

    def read(self):
        return self.content  # 读取文件内容

    def write(self, content):
        self.content = content  # 写入文件内容

class Folder(Node):
    def __init__(self, name):
        super(Folder, self).__init__(name)  # 初始化父类属性
        self.children = {}  # 子节点字典

    def add_child(self, child_node):
        child_node.parent = self  # 设置子节点的父节点为当前文件夹
        self.children[child_node.name] = child_node  # 添加子节点到字典中

    def remove_child(self, name):
        if name in self.children:
            del self.children[name]  # 删除子节点

    def get_child(self, name):
        return self.children.get(name)  # 获取子节点

    def list_children(self):
        return list(self.children.keys())  # 列出所有子节点名称

class FileSystem:
    def __init__(self):
        self.root = Folder('/')  # 根文件夹

    def create_file(self, path, content=''):
        parts = path.strip('/').split('/')  # 分割路径
        parent_folder = self._get_parent_folder(parts[:-1])  # 获取父文件夹
        file_name = parts[-1]
        new_file = File(file_name, content)  # 创建新文件
        parent_folder.add_child(new_file)  # 将文件添加到父文件夹

    def create_folder(self, path):
        parts = path.strip('/').split('/')  # 分割路径
        parent_folder = self._get_parent_folder(parts[:-1])  # 获取父文件夹
        folder_name = parts[-1]
        new_folder = Folder(folder_name)  # 创建新文件夹
        parent_folder.add_child(new_folder)  # 将文件夹添加到父文件夹

    def _get_parent_folder(self, parts):
        current_folder = self.root  # 从根文件夹开始
        for part in parts:
            if part not in current_folder.children:
                raise ValueError(f"Path does not exist: {'/'.join(parts)}")  # 路径不存在
            current_folder = current_folder.children[part]
            if not isinstance(current_folder, Folder):
                raise ValueError(f"Not a directory: {'/'.join(parts)}")  # 不是文件夹
        return current_folder

    def delete(self, path):
        parts = path.strip('/').split('/')  # 分割路径
        parent_folder = self._get_parent_folder(parts[:-1])  # 获取父文件夹
        name = parts[-1]
        if name in parent_folder.children:
            del parent_folder.children[name]  # 删除子节点
        else:
            raise ValueError(f"Path does not exist: {path}")  # 路径不存在

    def move(self, src_path, dest_path):
        src_parts = src_path.strip('/').split('/')  # 分割源路径
        src_name = src_parts[-1]
        src_parent_folder = self._get_parent_folder(src_parts[:-1])  # 获取源父文件夹

        dest_parts = dest_path.strip('/').split('/')  # 分割目标路径
        dest_name = dest_parts[-1]
        dest_parent_folder = self._get_parent_folder(dest_parts[:-1])  # 获取目标父文件夹

        if src_name not in src_parent_folder.children:
            raise ValueError(f"Source path does not exist: {src_path}")  # 源路径不存在

        node_to_move = src_parent_folder.children[src_name]  # 获取要移动的节点
        del src_parent_folder.children[src_name]  # 从源父文件夹删除节点

        if dest_name in dest_parent_folder.children:
            raise ValueError(f"Destination already exists: {dest_path}")  # 目标已存在

        dest_parent_folder.add_child(node_to_move)  # 将节点添加到目标父文件夹

    def rename(self, path, new_name):
        parts = path.strip('/').split('/')  # 分割路径
        name = parts[-1]
        parent_folder = self._get_parent_folder(parts[:-1])  # 获取父文件夹

        if name not in parent_folder.children:
            raise ValueError(f"Path does not exist: {path}")  # 路径不存在

        node_to_rename = parent_folder.children[name]  # 获取要重命名的节点
        del parent_folder.children[name]  # 从父文件夹删除旧节点名称
        node_to_rename.name = new_name  # 修改节点名称
        parent_folder.add_child(node_to_rename)  # 将节点添加回父文件夹

    def get_node(self, path):
        parts = path.strip('/').split('/')  # 分割路径
        current_folder = self.root  # 从根文件夹开始
        for part in parts:
            if part not in current_folder.children:
                raise ValueError(f"Path does not exist: {'/'.join(parts)}")  # 路径不存在
            current_folder = current_folder.children[part]
        return current_folder  # 返回找到的节点

    def print_tree(self, node=None, level=0):
        if node is None:
            node = self.root  # 默认从根文件夹开始
        indent = '  ' * level  # 缩进级别
        print(f"{indent}{node.name}/" if isinstance(node, Folder) else f"{indent}{node.name}")  # 打印节点名称
        if isinstance(node, Folder):
            for child in sorted(node.children.values(), key=lambda x: (isinstance(x, File), x.name)):  # 排序子节点
                self.print_tree(child, level + 1)  # 递归打印子节点

# 示例用法
if __name__ == "__main__":
    fs = FileSystem()  # 创建文件系统实例
    fs.create_folder('home')  # 创建文件夹 home
    fs.create_folder('home/user')  # 创建文件夹 home/user
    fs.create_file('home/user/file1.txt', 'Hello, World!')  # 创建文件 home/user/file1.txt
    fs.create_file('home/user/file2.txt', 'Another file.')  # 创建文件 home/user/file2.txt
    fs.create_folder('home/user/documents')  # 创建文件夹 home/user/documents
    fs.create_file('home/user/documents/report.pdf', 'Report content.')  # 创建文件 home/user/documents/report.pdf

    print("Initial tree:")
    fs.print_tree()  # 打印初始文件树

    print("\nRenaming file1.txt to myfile.txt:")
    fs.rename('home/user/file1.txt', 'myfile.txt')  # 重命名文件
    fs.print_tree()  # 打印重命名后的文件树

    print("\nMoving report.pdf to home:")
    fs.move('home/user/documents/report.pdf', 'home/report.pdf')  # 移动文件
    fs.print_tree()  # 打印移动后的文件树

    print("\nDeleting file2.txt:")
    fs.delete('home/user/file2.txt')  # 删除文件
    fs.print_tree()  # 打印删除后的文件树